import os

from bs4 import BeautifulSoup


# 解析html获得标题
def get_title(chapter_source_code):
    soup = BeautifulSoup(chapter_source_code, 'html.parser')
    h1_tags = soup.find_all('h1')
    title_list = []
    for i in h1_tags:
        title_list.append(i.contents[0])
    title = title_list[0]
    return title


# 解析html获得内容
def get_content(chapter_source_code):
    content_list = []
    soup = BeautifulSoup(chapter_source_code, 'html.parser')
    h1_tags = soup.find_all('p')
    for i in h1_tags:
        content_list.append(i.contents[0].text.strip())
    content = '\n'.join(content_list)
    return content


# 找到所有分隔符所在行数
def find_separators(lines_list):
    return [i for i in range(len(lines_list)) if lines_list[i].startswith('=')]


# 根据分隔符切割文档为单章的文档
def split_document(lines_list, index_list):
    star_index = 0
    top_list = []
    for i in range(len(index_list)):
        end_index = index_list[i]
        top_list.append(lines_list[star_index: end_index])
        star_index = end_index + 1
    return top_list


# 写出单章
def write_chapter(top_list, out_path):
    title_list = []
    for l in top_list:
        chapter_source_code = ''.join(l)
        # 获取单章标题和内容
        title = get_title(chapter_source_code)
        title_list.append(title)
        content = get_content(chapter_source_code)
        # 写出单章和内容
        out_file = out_path + os.sep + title + '.txt'
        with open(out_file, 'w', encoding='utf-8') as f:
            f.write(title)
            f.write(os.linesep)
            f.write(content)
    # 所给文档有多少个单章（可以包含重复单章）
    print('所给文档有{}个单章（可以包含重复单章）。'.format(len(title_list)))
    # 重复的单章标题
    print('重复的单章标题如下：')
    print(list(set([x for x in title_list if title_list.count(x) > 1])))


if __name__ == '__main__':
    path = r'C:\Users\jimka\Downloads\doc\all.html'
    with open(path, mode='r', encoding='utf-8') as f:
        lines_list = f.readlines()

    index_list = find_separators(lines_list)
    top_list = split_document(lines_list, index_list)

    out_path = r'C:\Users\jimka\PycharmProjects\project-01\resources\消费系男神txt'
    write_chapter(top_list, out_path)

    print("程序运行完毕。")
